﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using CustomsSystem.Model;
using CustomsSystem.BLL;
using Maticsoft.Common;

namespace WEB.contact
{
    public partial class contactEdit : System.Web.UI.Page
    {
        ContactManage contManage = new ContactManage();
        //根据公司ID获取公司名字
        CustomerManage cusBll = new CustomerManage();


        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetCustomersName(string prefixText, int count)
        {
            CustomerManage cusManage = new CustomerManage();
            List<string> strArray = new List<string>();
            //string[] strArray = { "中国石油公司","中顺洁柔","中山技术",
            //"捷达捷达捷达达","时代复分","幅度为爱"};
            List<Customer> cusList = cusManage.GetModelList("");
            foreach (Customer aCus in cusList)
            {
                strArray.Add(aCus.CustomerName);
            }
            return (from m in strArray
                    where m.Contains(prefixText)
                    select m).Take(count).ToArray();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)   //修改
                {
                    int id = Convert.ToInt32(Request.QueryString["ID"]);

                    btnEdit1.Text = "修改";
                    /*载入数据*/
                    Contact myCont = contManage.GetModel(id);
                    Customer cusModel = cusBll.GetModel(Convert.ToInt32(myCont.Customer_ID));
                    tbCusID.Text = cusModel.CustomerName;
                    tbCusID.ReadOnly = true;
                    tbCPName.Text = myCont.CPName;
                    ddlSex.Text = myCont.CPSex;
                    tbCPPost.Text = myCont.CPPost;
                    tbMobilePhone.Text = myCont.MobilePhone;
                    tbCPPhone.Text = myCont.CPPhone;
                    tbEmail.Text = myCont.Email;
                    tbRemarks.Text = myCont.Remarks;
                    tbFax.Text=myCont .Fax;
                    /**/
                    Session["updateID"] = id;
                }
                else                            //新增
                {
                    if (Request.QueryString["AddID"] != null)//客户编号的绑定
                    {
                        string cusID = Request.QueryString["AddID"].ToString();//读取客户编号
                        Customer cusModel = cusBll.GetModel(Convert.ToInt32(cusID));//取客户名称
                        tbCusID.Text = cusModel.CustomerName;
                        tbCusID.ReadOnly = true;
                    }
                    btnEdit1.Text = "增加";

                    Session["updateID"] = -1;
                }
            }
        }

        #region 释放资源
        protected void Page_Unload(object sender, EventArgs e)
        {
            //if (null != Session["Customer_ID"])
            //{
            //    Session.Remove("Customer_ID");
            //}
            //if(null !=Session["updateID"])
            //{
            //    Session.Remove("updateID");
            //}
        }
        #endregion

        protected void btnEdit1_Click(object sender, EventArgs e)
        {
            Contact myCont = new Contact();
            myCont.ID = Convert.ToInt32(Session["updateID"]);
            //if (Session["Customer_ID"] != null)
            //{
            //    myCont.Customer_ID = Convert.ToInt32(Session["Customer_ID"]);
            //}
            if (myCont.Customer_ID != null)
            {
                myCont.Customer_ID = myCont.Customer_ID;
            }
            else
            {
                //根据公司名字获取公司ID
                List<Customer> cusModels = cusBll.GetModelList(" CustomerName= '"+tbCusID.Text.Trim()+"' ");
                if (cusModels.Count != 0)
                {
                    myCont.Customer_ID = cusModels[0].ID;
                }
                else
                {
                    myCont.Customer_ID = 0;
                    MessageBox.Show(this, "该公司不存在，请确认"); //error["cusID"] = "该公司不存在，请确认";
                    return;
                }
            }
            myCont.CPName = tbCPName.Text.Trim();
            //性别下拉框的读取
            if (ddlSex.Text == "男")
            {
                myCont.CPSex = "0";
            }
            else
            {
                myCont.CPSex = "1";
            }
            //信息录入model
            myCont.CPPost = tbCPPost.Text.Trim();
            myCont.MobilePhone = tbMobilePhone.Text.Trim();
            myCont.CPPhone =tbCPPhone.Text.Trim();
            myCont.Fax=tbFax.Text.Trim();
            myCont.Email = tbEmail.Text.Trim();
            myCont.Remarks = tbRemarks.Text.Trim();

            //手机号码验证
            
            string str = "";
            if (tbCPName.Text.Trim() == "")
            {
                str += "  姓名不能为空!\\n";

            }
            #region 暂存验证
            //string Validation1 = tbMobilePhone.Text.Trim();
            //Regex MobilePhone = new Regex(@"^(?<国家代码>(\+86)|(\(\+86\)))?(?<手机号>((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\d{8})$");
            //if (!MobilePhone.IsMatch(Validation1)&&Validation1!="")
            //{
            //    str+="  手机号码格式错误!\\n";
            //    //tbMobilePhone.BorderColor = System.Drawing.Color.Red;
            //    tbMobilePhone.Text = "";
                
            //}
            //string Validation2 = myCont.CPPhone;
            //Regex CPPhone = new Regex(@"(^(?<区号>(\d{3,4}-)|(\(\d{3,4}-))?\d{7,8})");
            //if (!CPPhone.IsMatch(Validation2) && Validation2 != "")
            //{
            //    str+="  固定电话号码格式错误!\\n";
            //    //tbCPPhone.BorderColor = System.Drawing.Color.Red;
            //    tbCPPhone.Text = "";
            //}
            //if (!CPPhone.IsMatch(tbFax.Text.Trim()) && tbFax.Text.Trim() != "")
            //{
            //    str += "  传真号码格式错误!\\n";
            //    //tbCPPhone.BorderColor = System.Drawing.Color.Red;
            //    tbCPPhone.Text = "";
            //}
            ////电子邮箱
            //if (!PageValidate.IsEmail(tbEmail.Text.Trim()) && tbEmail.Text.Trim() != "")
            //{
            //    str += "  电子邮箱错误!\\n";
            //        //tbEmail.BorderColor=System.Drawing.Color.Red;
            //    tbEmail.Text = "";
            //}
            #endregion
            
            if (tbRemarks.Text.Trim().Length >= 100)
            {
                str += "  备注文字数超过规定的长度!";
            }//备注验证

            if (str != "")
            {
                str = "注意：\\n" + str;
                MessageBox.Show(this, str);
                return;
            }

            if (myCont.ID == -1 )//增加记录
            {
                if (contManage.Add(myCont))
                {
                    if (Session["Customer_ID"] != null)//客户编号的绑定
                    {
                        Session["Customer_ID"] = null;
                    }
                    MessageBox.ShowAndRedirect(this, "增加成功", "./contactEdit.aspx?AddID=" + myCont.Customer_ID);
                    //数据增加成功
                }
                else
                {
                    Response.Write("<script>alert('增加失败!')</script>");
                }
            }
            else   //修改记录
            {
                if (contManage.Update(myCont))
                {
                   MessageBox.ShowAndRedirect(this, "更新成功", "/customer/customerView.aspx");
                }
                else
                {
                    Response.Write("<script>alert('更新失败!')</script>");
                }
            }
        }

        protected void btnExit_Click(object sender, EventArgs e)
        {
            
            Response.Redirect("/customer/customerView.aspx");//返回键
        }
    }
}
